package cn.detu.ln.service.impl;

import cn.detu.ln.entity.Teacher;
import cn.detu.ln.mapper.TeacherMapper;
import cn.detu.ln.service.TeacherService;
import cn.detu.ln.utils.RestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public RestUtil selectAll() {
        return new RestUtil(200,"",teacherMapper.selectList(null));
    }

    @Override
    public RestUtil selectOne(Integer teaId) {
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("tea_id",teaId));
        if(teacher == null) return new RestUtil(500,"用户不存在！",null);
        return new RestUtil(200,"查询成功！",teacher);
    }

    @Override
    public RestUtil login(Teacher teacher) {
        // 根据用户名查询Admin
        Teacher resUser = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("tea_name",teacher.getTeaName()));
        if(resUser == null ) return new RestUtil(500,"用户名不存在，请联系管理员！",null);
        // 用户名存在，判断密码是否正确
        return resUser.getTeaPwd().equals(teacher.getTeaPwd()) ?
                new RestUtil(200,"恭喜，登陆成功！",null) :
                new RestUtil(500,"密码错误，请重试！",null);
    }

    @Override
    public RestUtil teacherUpdatePwd(Teacher teacher, String newPwd) {
        // 判断新密码与旧密码不能一样
        System.out.println(teacher.getTeaPwd());
        System.out.println(newPwd);
        if(teacher.getTeaPwd().equals(newPwd))  return new RestUtil(500,"新密码与旧密码不能一样，请重试！",null);
        teacher.setTeaPwd(newPwd);
        return teacherMapper.updateById(teacher) >0 ?new RestUtil(200,"恭喜，密码已修改成功！",null) : new RestUtil(500,
                "密码修改失败，请重试！",
                null);
    }

    @Override
    public RestUtil select(Integer teaId) {
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("tea_id",teaId));
        if(teacher == null) return new RestUtil(500,"用户不存在！",null);
        return new RestUtil(200,"查询成功！",teacher);
    }

    @Override
    public Teacher queryTeacherById(Integer teaId) {
        return teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("tea_id",teaId));
    }

    @Override
    public RestUtil teacherDelete(Integer teaId) {
        return teacherMapper.deleteById(teaId)>0?new RestUtil(200,"删除成功！",null):new RestUtil(500,"删除失败",null);
    }

    @Override
    public RestUtil teacherUpdate(Teacher teacher) {
        return teacherMapper.updateById(teacher)>0?new RestUtil(200,"修改成功！",null):new RestUtil(500,"修改失败",null);
    }

    @Override
    public RestUtil teacherInsert(Teacher teacher) {
        return teacherMapper.insert(teacher)>0?new RestUtil(200,"恭喜，该师录入成功！",null):new RestUtil(500,"添加失败",null);
    }

    @Override
    public RestUtil queryTeacherByPage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Teacher> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Teacher>().like("tea_name",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Teacher> teacherPage = teacherMapper.selectPage(new Page<Teacher>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",teacherPage.getTotal());// 总条数
        map.put("pages",teacherPage.getPages());// 总页数
        map.put("teachers",teacherPage.getRecords());// 当前页数据
        return teacherPage != null ? new RestUtil(200,"",map) : null;
    }

    @Override
    public RestUtil queryTeacherByTeaIdPage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Teacher> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Teacher>().like("tea_id",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Teacher> teacherPage = teacherMapper.selectPage(new Page<Teacher>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",teacherPage.getTotal());// 总条数
        map.put("pages",teacherPage.getPages());// 总页数
        map.put("teachers",teacherPage.getRecords());// 当前页数据
        return teacherPage != null ? new RestUtil(200,"",map) : null;
    }

    @Override
    public RestUtil queryTeacherByTeaTitlePage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Teacher> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Teacher>().like("tea_title",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Teacher> teacherPage = teacherMapper.selectPage(new Page<Teacher>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",teacherPage.getTotal());// 总条数
        map.put("pages",teacherPage.getPages());// 总页数
        map.put("teachers",teacherPage.getRecords());// 当前页数据
        return teacherPage != null ? new RestUtil(200,"",map) : null;
    }
}
